import {
	PortletActionType,
	ICtrlEvent,
	IPortletAbility,
	IPortletControllerParams,
	IPortletModel,
	IPortletStore,
	IParam,
	IViewAbility,
} from '@/core';
import { QXEvent } from 'qx-util';
import {
	getCtrlClassNames,
	useCtrlAction,
	useCtrlBasic,
	handleCtrlDestroy,
	handleCtrlInit,
	handleCtrlAction,
	useCtrlControllerParams,
	handleToolbarItemClick,
	handleComponentAction,
	useCtrlProps,
} from '../use-ctrl';

/**
 * 门户部件输入参数
 *
 * @export
 * @return {*}
 */
export function usePortletProps() {
	return {
		...useCtrlProps(),
		openView: Function,
		newView: Function,
	};
}

/**
 * 门户部件基础参数
 *
 * @export
 * @param {IPortletModel} model
 * @param {IParam} props
 * @return {*}
 */
export function usePortletBasic(model: IPortletModel, props: IParam) {
	const getClassNames = (props: IParam) => {
		return getCtrlClassNames(model, props);
	};
	return {
		...useCtrlBasic(model, props),
		getClassNames,
		handleCtrlDestroy,
		handleCtrlInit,
		handleCtrlAction,
		handleToolbarItemClick,
		handleComponentAction,
	};
}

/**
 * 门户部件部件行为
 *
 * @export
 */
export function usePortletAction(controller: any) {
	const handleViewInit = (name: string, data: IViewAbility) => {
		controller.setSubAbility(name, data);
	};
	return {
		...useCtrlAction(controller),
		handleViewInit,
	};
}

/**
 * 门户部件部件控制器
 *
 * @export
 */
export function usePortletControllerParams<
	S extends IPortletStore,
	M extends IPortletModel
>(
	model: M,
	props: any,
	evt: QXEvent<ICtrlEvent<PortletActionType, IPortletAbility>>,
	otherParams: IParam = {}
): IPortletControllerParams<PortletActionType, IPortletAbility> {
	return {
		...useCtrlControllerParams<S, M>(model, props, evt, otherParams),
		openView: props.openView,
		newView: props.newView,
	};
}
